/*!
 * \file mongodb_core.c
 *
 * \brief 共通定义
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 * 共通使用的宏和结构体
 *
 * \version 1.0
 * \author jikw
 * \date 2015/12/25
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "frservice_comm.h"
#include "frbyte_buffer.h"
#include "mongodb_core.h"

extern void *g_p_apm_context;

#define MONGODB_DOCUMENTS_LEN_MAX             1048576//最大长度上限1024*1024

#define MONGODB_DATA_TYPE_DOUBLE              0x01//定义各数据类型
#define MONGODB_DATA_TYPE_STRING              0x02
#define MONGODB_DATA_TYPE_OBJECT              0x03
#define MONGODB_DATA_TYPE_ARRAY               0x04
#define MONGODB_DATA_TYPE_BINARY_DATA         0x05
#define MONGODB_DATA_TYPE_OBJECT_ID           0x07
#define MONGODB_DATA_TYPE_BOOLEAN             0x08
#define MONGODB_DATA_TYPE_DATE                0x09
#define MONGODB_DATA_TYPE_NULL                0x0a
#define MONGODB_DATA_TYPE_32_BIT_INTEGER      0x10
#define MONGODB_DATA_TYPE_64_BIT_INTEGER      0x12

union int64_double_bits {
    int64_t int_bits;
    double double_bits;
};

typedef struct __st_L7session_mongodb_private
{
    unsigned long ul_seq;                             //序列号，1次req和response时，使用同一个序列号
    L4_transaction st_l4_trans;                       //数据
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    Frbyte_Packet *p_req_packet;                      //存请求bytebuff数据
    Frbyte_Packet *p_resp_packet;                     //存相应bytebuff数据
    int i_len;                                        //数据总长度
    int i_req_doc_len;                                //documents长度
    int i_resp_doc_len;                               //documents长度
    int i_sign;                                       //标记位
}L7session_mongodb_private;

/*!
 * mongodb私有数据创建
 *
 * \param void
 *
 * \return NULL          内存分配失败\n
 *         p_st_private  内存分配成功
 *
 * \note
 *
 */
FR_STATIC L7session_mongodb_private* __mongodb_private_create(void)
{
    L7session_mongodb_private *p_st_private = NULL;
    p_st_private = (L7session_mongodb_private*)frmalloc(sizeof(L7session_mongodb_private));
    if (p_st_private == NULL)
        return NULL;
    memset(p_st_private, 0x00, sizeof(L7session_mongodb_private));

    return p_st_private;
}

/*!
 * 私有信息重置
 *
 * \param p_private_session  (mongodb的私有session数据)
 *
 * \return FR_SUCC   成功\n
 *
 * \note
 *
 */
FR_STATIC int __mongodb_session_reset(void *p_private_session)
{
    L7session_mongodb_private *p_st_private = (L7session_mongodb_private*)p_private_session;

    if (p_st_private)
    {
        if (p_st_private->p_st_body_data_req)
            frreport_body_free(&p_st_private->p_st_body_data_req);

        if (p_st_private->p_st_body_data_resp)
            frreport_body_free(&p_st_private->p_st_body_data_resp);

        if (p_st_private->p_lst_req)
            frreport_body_l7list_free(&p_st_private->p_lst_req);

        if (p_st_private->p_lst_comm)
            frreport_body_l7list_free(&p_st_private->p_lst_comm);

        if (p_st_private->p_req_packet)
            bytebuff_free(&(p_st_private->p_req_packet));

        if (p_st_private->p_resp_packet)
            bytebuff_free(&(p_st_private->p_resp_packet));

    }
    return FR_SUCC;
}

/*!
 * 判断是否是mongodb协议
 *
 * \param p_st_conn      (输入数据信息)
 * \param p_st_packet    (输入数据)
 *
 * \return APM_L7_IS_YES  属于mongodb协议\n
 *         APM_L7_IS_NO   不属于mongodb协议
 *
 * \note
 *
 */
int mongodb_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet)
{
    int i_ret = 0;
    //unsigned int ui_len = 0;
    uint32_t u32_len = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;

    if (p_st_packet == NULL || p_st_packet->p_payload == NULL ||
        p_st_packet->ui_payload_len < 16 || p_st_conn==NULL)
        return APM_L7_IS_NO;

    //ui_len = p_st_packet->ui_payload_len;           //packet数据长度
    p_data = p_st_packet->p_payload;                //packet数据内容

    //创建bytebuff,将数据块前16位添加到bytebuff。
    p_packet = bytebuff_creat();
    if (p_packet == NULL)
        goto ERROR;
    if (bytebuff_add(p_packet, p_data, 16) != FR_SUCC)
        goto ERROR;
    //跳12位跳过Message Length、Request ID、Response To
    if (bytebuff_skip(p_packet, 12) != FR_SUCC)
        goto ERROR;
    //13 14 15 16位表示OpCode
    if (bytebuff_get_int(p_packet, 4, &u32_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
        goto ERROR;
    if (u32_len != 2001 && u32_len != 2002 && u32_len != 2004 &&
    u32_len != 2005 && u32_len != 2006 && u32_len != 2007)
        goto ERROR;

    i_ret = frct_get_direct_known(p_st_conn);
    if (i_ret == FR_DIRECT_UNKNOWN)
        frcap_set_cs_srv(p_st_conn, p_st_packet->dst_ip, p_st_packet->dst_port);
    frservice_add(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0);
    if (p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return APM_L7_IS_YES;

ERROR:
    if (p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    if (frct_get_direct_known(p_st_conn) == FR_DIRECT_UNKNOWN)
    {
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
        {
            frcap_set_cs_srv(p_st_conn, p_st_packet->dst_ip, p_st_packet->dst_port);
            return APM_L7_IS_YES;
        }
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
        {
            frcap_set_cs_srv(p_st_conn, p_st_packet->src_ip, p_st_packet->src_port);
            return APM_L7_IS_YES;
        }
    }
    else
    {
        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
            return APM_L7_IS_YES;

        i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0, NULL);
        if (i_ret == FR_SUCC)
            return APM_L7_IS_YES;
    }
    return APM_L7_IS_NO;
}

/*!
 * 参数根据不同类型上报
 *
 * \param p_packet        (数据)
 * \param ui_data_len     (数据长度)
 * \param p_st_body_data
 * \param i_type          (标志位,非0:request,0:response)
 *
 * \return FR_SUCC              成功\n
 *         FR_FAIL              失败
 *         APM_L7_ANALYZE_TODO
 *
 * \note
 *
 */
FR_STATIC int __mongodb_add_args(Frbyte_Packet *p_st_packet
                                       , unsigned int ui_data_len
                                       , Frreport_body_data *p_st_body_data
                                       , int i_type
                                       , int i_count)
{
    int i_tmp = 0;
    int i_index = 0;
    char arr_data[13];
    char arr_type[2];
    char *p_full_name = NULL;
    char *p_element = NULL;
    char *p_value = NULL;
    int32_t i32_value = 0;
    int64_t i64_value = 0;
    uint32_t u32_len = 0;
    uint64_t u64_value = 0;
    union int64_double_bits u_64bits;
    double db_value = 0.0;
    Frbyte_Packet *p_packet = NULL;

    if (p_st_packet == NULL || ui_data_len < 1 || p_st_body_data == NULL)
        goto ERROR;

    p_packet = p_st_packet;
    memset(arr_data, 0x00, sizeof(arr_data));
    memset(arr_type, 0x00, sizeof(arr_type));

    if (bytebuff_remaining(p_packet) < 1)
    {
        if (p_value)
            frfree(p_value);
        if (p_element)
            frfree(p_element);
        if (p_full_name)
            frfree(p_full_name);
        return APM_L7_ANALYZE_TODO;
    }
    //类型保存在arr_type
    if (bytebuff_get_bytes(p_packet, arr_type, 1) == FR_FAIL)
        goto ERROR;
    i_tmp = bytebuff_getPosition(p_packet);
    if (i_tmp == -1)
        goto ERROR;
    if (bytebuff_memstr(p_packet, i_tmp, 0, "\x00", 1, &i_index) == FR_FAIL)
    {
        if (p_value)
            frfree(p_value);
        if (p_element)
            frfree(p_element);
        if (p_full_name)
            frfree(p_full_name);
        return APM_L7_ANALYZE_TODO;
    }
    if (i_index-i_tmp+1 <= 1)
        goto ERROR;
    p_element = (char*)frmalloc(i_index-i_tmp+1);
    if (p_element == NULL)
        goto ERROR;
    memset(p_element, 0x00, i_index-i_tmp+1);
    if (bytebuff_remaining(p_packet) < i_index-i_tmp+1)
    {
        if (p_value)
            frfree(p_value);
        if (p_element)
            frfree(p_element);
        if (p_full_name)
            frfree(p_full_name);
        return APM_L7_ANALYZE_TODO;
    }
    if (bytebuff_get_bytes(p_packet, p_element, i_index-i_tmp) == FR_FAIL)
        goto ERROR;

    p_full_name = (char*)frmalloc(i_index-i_tmp+1+30);
    if (p_full_name == NULL)
        goto ERROR;
    memset(p_full_name, 0x00, i_index-i_tmp+1+30);//26
    if (i_type == 0)
        snprintf(p_full_name, i_index-i_tmp+1+30, "response.mongodb.document%d.%s", i_count, p_element);
    else if (i_type == 1)//用作2001第二个document
        snprintf(p_full_name, i_index-i_tmp+1+23, "request.mongodb.update.%s", p_element);
    else if (i_type == 2002)
        snprintf(p_full_name, i_index-i_tmp+1+25, "request.mongodb.document.%s", p_element);
    else if (i_type == 2004)
        snprintf(p_full_name, i_index-i_tmp+1+22, "request.mongodb.query.%s", p_element);
    else if (i_type == 2001 || i_type == 2006)
        snprintf(p_full_name, i_index-i_tmp+1+25, "request.mongodb.selector.%s", p_element);
    //跳1位跳过\x00
    if (bytebuff_skip(p_packet, 1) != FR_SUCC)
        goto ERROR;

    switch (*arr_type)
    {
        case MONGODB_DATA_TYPE_DOUBLE:
            if (bytebuff_remaining(p_packet) < 8)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_long(p_packet, 8, &u64_value, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            u_64bits.int_bits = u64_value;
            db_value = u_64bits.double_bits;
            if (frreport_body_l7_add_double(p_st_body_data, p_full_name, db_value) == FR_FAIL)
                goto ERROR;
            break;
        case MONGODB_DATA_TYPE_STRING:
            if (bytebuff_remaining(p_packet) < 4)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_int(p_packet, 4, &u32_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            p_value = (char*)frmalloc(u32_len+1);
            if (p_value == NULL)
                goto ERROR;
            if ((uint32_t)bytebuff_remaining(p_packet) < u32_len)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_bytes(p_packet, p_value, u32_len) == FR_FAIL)
                goto ERROR;
            p_value[u32_len] = '\0';
            if (frreport_body_l7_add_str(p_st_body_data, p_full_name, p_value) == FR_FAIL)
                goto ERROR;
            frfree(p_value);
            p_value = NULL;
            break;
        case MONGODB_DATA_TYPE_OBJECT:
        case MONGODB_DATA_TYPE_ARRAY:
            if (bytebuff_remaining(p_packet) < 4)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_int(p_packet, 4, &u32_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            if (u32_len-4+1 <= 0)
                goto ERROR;
            p_value = (char*)frmalloc(u32_len-4+1);
            if (p_value == NULL)
                goto ERROR;
            if ((uint32_t)bytebuff_remaining(p_packet) < u32_len-4)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_bytes(p_packet, p_value, u32_len-4) == FR_FAIL)
                goto ERROR;
            p_value[u32_len-4] = '\0';
            if (frreport_body_l7_add_bin(p_st_body_data, p_full_name, p_value, u32_len-4) == FR_FAIL)
                goto ERROR;
            frfree(p_value);
            p_value = NULL;
            break;
        case MONGODB_DATA_TYPE_BINARY_DATA:
            if (bytebuff_remaining(p_packet) < 5)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_int(p_packet, 4, &u32_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            //跳1位跳过\x00
            if (bytebuff_skip(p_packet, 1) != FR_SUCC)
                goto ERROR;
            p_value = (char*)frmalloc(u32_len+1);
            if (p_value == NULL)
                goto ERROR;
            if ((uint32_t)bytebuff_remaining(p_packet) < u32_len)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_bytes(p_packet, p_value, u32_len) == FR_FAIL)
                goto ERROR;
            p_value[u32_len] = '\0';
            if (frreport_body_l7_add_bin(p_st_body_data, p_full_name, p_value, u32_len) == FR_FAIL)
                goto ERROR;
            frfree(p_value);
            p_value = NULL;
            break;
        case MONGODB_DATA_TYPE_OBJECT_ID:
            if (bytebuff_remaining(p_packet) < 12)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_bytes(p_packet, arr_data, 12) == FR_FAIL)
                goto ERROR;
            if (frreport_body_l7_add_bin(p_st_body_data, p_full_name, arr_data, 12) == FR_FAIL)
                goto ERROR;
            break;
        case MONGODB_DATA_TYPE_BOOLEAN:
            if (bytebuff_remaining(p_packet) < 1)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_int(p_packet, 1, &u32_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            if (u32_len == 0 || u32_len == 1)
            {
                if (frreport_body_l7_add_int(p_st_body_data, p_full_name, u32_len) == FR_FAIL)
                    goto ERROR;
            }
            else
                goto ERROR;
            break;
        case MONGODB_DATA_TYPE_DATE:
            if (bytebuff_remaining(p_packet) < 8)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_long(p_packet, 8, (uint64_t *)&i64_value, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            if (frreport_body_l7_add_int(p_st_body_data, p_full_name, i64_value) == FR_FAIL)
                goto ERROR;
            break;
        case MONGODB_DATA_TYPE_NULL:
            if (frreport_body_l7_add_str(p_st_body_data, p_full_name, "") == FR_FAIL)
                goto ERROR;
            break;
        case MONGODB_DATA_TYPE_32_BIT_INTEGER:
            if (bytebuff_remaining(p_packet) < 4)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_int(p_packet, 4, (uint32_t *)&i32_value, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            if (frreport_body_l7_add_int(p_st_body_data, p_full_name, i32_value) == FR_FAIL)
                goto ERROR;
            break;
        case MONGODB_DATA_TYPE_64_BIT_INTEGER:
            if (bytebuff_remaining(p_packet) < 8)
            {
                if (p_value)
                    frfree(p_value);
                if (p_element)
                    frfree(p_element);
                if (p_full_name)
                    frfree(p_full_name);
                return APM_L7_ANALYZE_TODO;
            }
            if (bytebuff_get_long(p_packet, 8, (uint64_t *)&i64_value, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            if (frreport_body_l7_add_int(p_st_body_data, p_full_name, i64_value) == FR_FAIL)
                goto ERROR;
            break;
        default:
            goto ERROR;
    }
    if (p_value)
        frfree(p_value);
    if (p_element)
        frfree(p_element);
    if (p_full_name)
        frfree(p_full_name);
    return FR_SUCC;

ERROR:
    if (p_value)
        frfree(p_value);
    if (p_element)
        frfree(p_element);
    if (p_full_name)
        frfree(p_full_name);
    return FR_FAIL;
}

/*!
 * req数据解析
 *
 * \param p_packet  (req数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 * \param p_st_conn       (入力链接信息)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *         APM_L7_ANALYZE_TODO
 *
 * \note
 *
 */
FR_STATIC int __req_parse(Frbyte_Packet *p_packet
                        , L7session_mongodb_private *p_st_private
                        , Frreport_body_data *p_st_body_data
                        , Conntrack *p_st_conn)
{
    int i_tmp = 0;
    int i_index = 0;
    int i_len = 0 ;
    int i_ret = 0;
    uint32_t u32_op_type = 0;
    uint32_t u32_doc_len = 0;
    char arr_len[5];
    char arr_tmp[2];
    char *p_value = NULL;

    if (p_packet == NULL || p_st_private == NULL || p_st_body_data == NULL || p_st_conn == NULL)
        return APM_L7_ANALYZE_FAIL;

    memset(arr_len, 0x00, sizeof(arr_len));
    memset(arr_tmp, 0x00, sizeof(arr_tmp));

    if (bytebuff_remaining(p_packet) < 20)
        goto ERROR;
    //跳四位跳过Message Length,跳四位跳过Request ID,跳四位跳过Response To
    if (bytebuff_skip(p_packet, 12) != FR_SUCC)
        goto ERROR;
    //13 14 15 16位表示OpCode: Query (2004)
    if (bytebuff_get_int(p_packet, 4, &u32_op_type, BYTE_LITTLE_ENDIAN) == FR_FAIL)
        goto ERROR;
    if (u32_op_type != 2001 && u32_op_type != 2002 && u32_op_type != 2004 &&
    u32_op_type != 2005 && u32_op_type != 2006 && u32_op_type != 2007)
        goto ERROR;

    switch(u32_op_type)
    {
        case 2001:
            if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "UPDATE") == FR_FAIL)
                goto ERROR;
            break;
        case 2002:
            if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "INSERT") == FR_FAIL)
                goto ERROR;
            break;
        case 2004:
            if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "QUERY") == FR_FAIL)
                goto ERROR;
            break;
        case 2005:
            if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "GET MORE") == FR_FAIL)
                goto ERROR;
            break;
        case 2006:
            if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "DELETE") == FR_FAIL)
                goto ERROR;
            break;
        case 2007:
            if (frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "KILL CURSORS") == FR_FAIL)
                goto ERROR;
            break;
        default:
            goto ERROR;
    }

    if (u32_op_type == 2004 || u32_op_type == 2005)
    {
        //ResponseRequired
        if (frreport_body_l7_add_int(p_st_body_data, APM_REPORT_KEY_REQ_ISRESPREQUIRED, 1) == FR_FAIL)
            goto ERROR;
        p_st_private->i_sign = 1;
    }
    else
    {
        //ResponseRequired
        if (frreport_body_l7_add_int(p_st_body_data, APM_REPORT_KEY_REQ_ISRESPREQUIRED, 0) == FR_FAIL)
            goto ERROR;
        p_st_private->i_sign = 0;
    }

    //跳四位跳过Query Flags
    if (bytebuff_skip(p_packet, 4) != FR_SUCC)
        goto ERROR;

    if (u32_op_type == 2007)
        return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);

    //搜索\x2e,英文句号。
    i_tmp = bytebuff_getPosition(p_packet);
    if (i_tmp == -1)
        goto ERROR;
    if (bytebuff_memstr(p_packet, i_tmp, 0, ".", 1, &i_index) == FR_FAIL)
        goto ERROR;
    //Database Name
    if (i_index-i_tmp+1 <= 1)
        goto ERROR;
    p_value = (char*)frmalloc(i_index-i_tmp+1);
    if (p_value == NULL)
        goto ERROR;
    if (bytebuff_remaining(p_packet) < i_index-i_tmp+1)
        goto ERROR;
    if (bytebuff_get_bytes(p_packet, p_value, i_index-i_tmp) == FR_FAIL)
        goto ERROR;
    p_value[i_index-i_tmp] = '\0';
    if (frreport_body_l7_add_str(p_st_body_data, "session.mongodb.database_name", p_value) == FR_FAIL)
        goto ERROR;
    frfree(p_value);
    p_value = NULL;
    //跳1位跳过'.'
    if (bytebuff_skip(p_packet, 1) != FR_SUCC)
        goto ERROR;
    //搜索\x00
    i_tmp = bytebuff_getPosition(p_packet);
    if (i_tmp == -1)
        goto ERROR;
    if (bytebuff_memstr(p_packet, i_tmp, 0, "\x00", 1, &i_index) == FR_FAIL)
        goto ERROR;
    //Collection Name
    if (i_index-i_tmp+1 <= 1)
        goto ERROR;
    p_value = (char*)frmalloc(i_index-i_tmp+1);
    if (p_value == NULL)
        goto ERROR;
    if (bytebuff_remaining(p_packet) < i_index-i_tmp+1)
        goto ERROR;
    if (bytebuff_get_bytes(p_packet, p_value, i_index-i_tmp) == FR_FAIL)
        goto ERROR;
    p_value[i_index-i_tmp] = '\0';
    if (frreport_body_l7_add_str(p_st_body_data, "session.mongodb.collection_name", p_value) == FR_FAIL)
        goto ERROR;
    frfree(p_value);
    p_value = NULL;
    //跳1位跳过\x00
    if (bytebuff_skip(p_packet, 1) != FR_SUCC)
        goto ERROR;

    if (u32_op_type == 2005)
        return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);
    if (u32_op_type == 2001 || u32_op_type == 2006)
    {
        //跳4位
        if (bytebuff_skip(p_packet, 4) != FR_SUCC)
            goto ERROR;
    }
    if (u32_op_type == 2004)
    {
        //跳8位跳过Number To Skip,Number to Return
        if (bytebuff_skip(p_packet, 8) != FR_SUCC)
            goto ERROR;
    }

    //自document起的总长度
    i_len = bytebuff_remaining(p_packet);
    if (bytebuff_remaining(p_packet) < 4)
        goto ERROR;
    //Document length
    if (bytebuff_get_int(p_packet, 4, &u32_doc_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
        goto ERROR;

    p_st_private->i_req_doc_len += u32_doc_len;
    if (p_st_private->i_req_doc_len > MONGODB_DOCUMENTS_LEN_MAX)
    {
        p_packet->ui_skip_len = p_st_private->i_len - p_packet->ui_all_len;
        return APM_L7_ANALYZE_TODO;
    }

    if (u32_doc_len == 5)
    {
        if (bytebuff_remaining(p_packet) < 1)
            goto ERROR;
        if (bytebuff_skip(p_packet, 1) != FR_SUCC)
            goto ERROR;
    }
    else
    {
        for (;;)
        {
            if (bytebuff_remaining(p_packet) == 0)
                break;
            i_ret = __mongodb_add_args(p_packet, bytebuff_remaining(p_packet), p_st_body_data, u32_op_type, 0);
            if (i_ret == FR_FAIL)
                goto ERROR;
            if (i_ret == APM_L7_ANALYZE_TODO)
                return i_ret;
            //i_len-u32_doc_len+1表示document以解析完，只剩document最后一位\x00
            if ((uint32_t)bytebuff_remaining(p_packet) == i_len-u32_doc_len+1)
            {
                if (bytebuff_get_bytes(p_packet, arr_tmp, 1) == FR_FAIL)
                    goto ERROR;
                if (arr_tmp[0] == '\x00')
                    break;
                else
                    goto ERROR;
            }
        }
    }

    if (u32_op_type == 2001)
    {
        //自document起的总长度
        i_len = bytebuff_remaining(p_packet);
        if (bytebuff_remaining(p_packet) < 4)
            goto ERROR;
        //Document length
        if (bytebuff_get_int(p_packet, 4, &u32_doc_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
            goto ERROR;

        p_st_private->i_req_doc_len += u32_doc_len;
        if (p_st_private->i_req_doc_len > MONGODB_DOCUMENTS_LEN_MAX)
        {
            p_packet->ui_skip_len = p_st_private->i_len - p_packet->ui_all_len;
            return APM_L7_ANALYZE_TODO;
        }

        if (u32_doc_len == 5)
        {
            if (bytebuff_remaining(p_packet) < 1)
                goto ERROR;
            if (bytebuff_skip(p_packet, 1) != FR_SUCC)
                goto ERROR;
        }
        else
        {
            for (;;)
            {
                if (bytebuff_remaining(p_packet) == 0)
                    break;
                i_ret = __mongodb_add_args(p_packet, bytebuff_remaining(p_packet), p_st_body_data, 1, 0);
                if (i_ret == FR_FAIL)
                    goto ERROR;
                if (i_ret == APM_L7_ANALYZE_TODO)
                    return i_ret;
                //i_len-u32_doc_len+1表示document以解析完，只剩document最后一位\x00
                if ((uint32_t)bytebuff_remaining(p_packet) == i_len-u32_doc_len+1)
                {
                    if (bytebuff_get_bytes(p_packet, arr_tmp, 1) == FR_FAIL)
                        goto ERROR;
                    if (arr_tmp[0] == '\x00')
                        break;
                    else
                        goto ERROR;
                }
            }
        }
    }

    return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);

ERROR:
    if (p_value)
        frfree(p_value);
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * resp数据解析
 *
 * \param p_packet   (resp数据)
 * \param p_st_private    (私有数据)
 * \param p_st_body_data
 * \param p_st_conn       (入力链接信息)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *         APM_L7_ANALYZE_TODO
 *
 * \note
 *
 */
FR_STATIC int __resp_parse(Frbyte_Packet *p_packet
                         , L7session_mongodb_private *p_st_private
                         , Frreport_body_data *p_st_body_data
                         , Conntrack *p_st_conn)
{
    int i_len = 0;
    int i_tmp = 0;
    int i_ret = 0;
    uint32_t u32_len = 0;
    uint32_t u32_doc_len = 0;
    char arr_len[5];
    char arr_tmp[2];

    if (p_packet == NULL || p_st_private == NULL ||
        p_st_body_data == NULL || p_st_conn == NULL)
        return APM_L7_ANALYZE_FAIL;

    memset(arr_len, 0x00, sizeof(arr_len));
    memset(arr_tmp, 0x00, sizeof(arr_tmp));

    if (bytebuff_remaining(p_packet) < 36)
        goto ERROR;
    //跳4位跳过Message Length,跳4位跳过Request ID,跳4位跳过Response To
    if (bytebuff_skip(p_packet, 12) != FR_SUCC)
        goto ERROR;
    //13 14 15 16位表示OpCode: OP_REPLY (1)
    if (bytebuff_get_int(p_packet, 4, &u32_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
        goto ERROR;
    if (u32_len != 1)
        goto ERROR;
    //跳4位跳过Query Flags,跳8位跳过Cursor ID,跳4位跳过Starting From
    if (bytebuff_skip(p_packet, 16) != FR_SUCC)
        goto ERROR;

    //Number Returned
    if (bytebuff_get_int(p_packet, 4, &u32_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
        goto ERROR;
    if (u32_len != 0)
    {
        for (i_tmp=0;(uint32_t)i_tmp<u32_len;i_tmp++)
        {
            //自document起的总长度
            i_len = bytebuff_remaining(p_packet);
            if (bytebuff_remaining(p_packet) < 4)
                return APM_L7_ANALYZE_TODO;
            //Document length
            if (bytebuff_get_int(p_packet, 4, &u32_doc_len, BYTE_LITTLE_ENDIAN) == FR_FAIL)
                goto ERROR;
            p_st_private->i_resp_doc_len += u32_doc_len;
            if (p_st_private->i_resp_doc_len > MONGODB_DOCUMENTS_LEN_MAX)
            {
                p_packet->ui_skip_len = p_st_private->i_len - p_packet->ui_all_len;
                return APM_L7_ANALYZE_TODO;
            }
            if (u32_doc_len == 5)
            {
                if (bytebuff_remaining(p_packet) < 1)
                    return APM_L7_ANALYZE_TODO;
                if (bytebuff_skip(p_packet, 1) != FR_SUCC)
                    goto ERROR;
            }
            else
            {
                for (;;)
                {
                    if (bytebuff_remaining(p_packet) == 0)
                        break;
                    i_ret = __mongodb_add_args(p_packet, bytebuff_remaining(p_packet), p_st_body_data, 0, i_tmp+1);
                    if (i_ret == FR_FAIL)
                        goto ERROR;
                    if (i_ret == APM_L7_ANALYZE_TODO)
                        return i_ret;
                    //i_len-u32_doc_len+1表示document以解析完，只剩document最后一位\x00
                    if ((uint32_t)bytebuff_remaining(p_packet) == i_len-u32_doc_len+1)
                    {
                        if (bytebuff_get_bytes(p_packet, arr_tmp, 1) == FR_FAIL)
                            goto ERROR;
                        if (arr_tmp[0] == '\x00')
                            break;
                        else
                            goto ERROR;
                    }
                }
            }
        }
    }
    return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);

ERROR:
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * mongodb的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet       (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 * \param p_st_session       (私有数据)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *         APM_L7_ANALYZE_TODO
 *
 * \note
 *
 */
FR_STATIC int __req_data_analysis(Conntrack *p_st_conn
                                , Apm_packet *p_st_packet
                                , Frhash_header *p_st_hash_report
                                , L7_session *p_st_session)
{
    int i_flag = 0;
    int i_ret = APM_L7_ANALYZE_SUCC;
    int *p_i_tmp = NULL;
    char arr_len[5];
    L7session_mongodb_private *p_st_private = NULL;
    L4_transaction *p_st_l4_trans = NULL;
    Frreport_data *p_st_report_data = NULL;
    unsigned long ul_tmp = 0;

    if (p_st_conn == NULL || p_st_packet == NULL || p_st_hash_report == NULL || p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    memset(arr_len, 0x00, sizeof(arr_len));

    p_st_private = (L7session_mongodb_private*)p_st_session->p_session_private;
    p_st_l4_trans = &(p_st_private->st_l4_trans);

    //如果连续的两个request(不算oneway request)
    if (p_st_private->p_lst_comm)
        __mongodb_session_reset((void *)(p_st_private));

    //创建数据链表
    if (p_st_private->p_st_body_data_req == NULL)
    {
        p_st_private->p_st_body_data_req = frreport_body_new_l7(p_st_conn);
        if (p_st_private->p_st_body_data_req == NULL)
            goto ERROR;

        if (frreport_body_l7_add_connInfo(p_st_private->p_st_body_data_req, p_st_conn) == FR_FAIL)
            goto ERROR;

        //seq ++
        p_st_private->ul_seq = frct_seq_increase(p_st_conn);

        if (p_st_l4_trans->st_request_time_first_byte.tv_sec==0 && p_st_l4_trans->st_request_time_first_byte.tv_usec==0)
        {
            //st_request_time_first_byte
            p_st_l4_trans->st_request_time_first_byte.tv_sec = p_st_packet->st_timestamp.tv_sec;
            p_st_l4_trans->st_request_time_first_byte.tv_usec = p_st_packet->st_timestamp.tv_usec;
        }

        //创建bytebuff
        if (p_st_private->p_req_packet == NULL)
        {
            p_st_private->p_req_packet = bytebuff_creat();
            if (p_st_private->p_req_packet == NULL)
                goto ERROR;
        }
    }
    /* 请求的字节数 */
    p_st_l4_trans->l_request_bytes += p_st_packet->ui_payload_len;

    if (p_st_private->p_req_packet == NULL)
        goto ERROR;
    if (p_st_private->p_req_packet->ui_remaining == 0)
    {
        arr_len[0] = *(p_st_packet->p_payload+3);
        arr_len[1] = *(p_st_packet->p_payload+2);
        arr_len[2] = *(p_st_packet->p_payload+1);
        arr_len[3] = *(p_st_packet->p_payload);
        p_i_tmp = (int *)arr_len;
        //预期数据总长度赋值给p_st_private->i_len
        p_st_private->i_len = ntohl(*p_i_tmp);
    }
    i_flag = p_st_private->p_req_packet->ui_skip_len;
    //把这个包的数据先全加进bytebuff里
    if (bytebuff_add(p_st_private->p_req_packet, p_st_packet->p_payload, p_st_packet->ui_payload_len) != FR_SUCC)
        goto ERROR;

    if (p_st_private->p_req_packet->ui_skip_len)
        return APM_L7_ANALYZE_TODO;
    if (i_flag == 0)
    {
        //req解析
        i_ret = __req_parse(p_st_private->p_req_packet, p_st_private, p_st_private->p_st_body_data_req, p_st_conn);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;
        if (i_ret == APM_L7_ANALYZE_TODO)
        {
            frreport_body_free(&p_st_private->p_st_body_data_req);
            bytebuff_rewind(p_st_private->p_req_packet);
            return i_ret;
        }
    }

    if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_ID, p_st_private->ul_seq) == FR_FAIL)
        goto ERROR;

    //start time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_START_TIMESTAMP, &p_st_l4_trans->st_request_time_first_byte) == FR_FAIL)
        goto ERROR;

    //st_request_time
    p_st_l4_trans->st_request_time.tv_sec = p_st_packet->st_timestamp.tv_sec;
    p_st_l4_trans->st_request_time.tv_usec = p_st_packet->st_timestamp.tv_usec;

    ul_tmp = frtime_diff(&(p_st_l4_trans->st_request_time_first_byte), &(p_st_l4_trans->st_request_time));
    //send_time_us
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_SEND_TIME_US, ul_tmp) == FR_FAIL)
        goto ERROR;

    //request bytes
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_DATA_BYTES, p_st_l4_trans->l_request_bytes) == FR_FAIL)
        goto ERROR;

    //end time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_END_TIMESTAMP, &p_st_l4_trans->st_request_time) == FR_FAIL)
        goto ERROR;

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);
    if (p_st_report_data == NULL)
        goto ERROR;

    if (p_st_private->i_sign == 1)
    {
        //拷贝相关链表
        p_st_private->p_lst_comm = FRREPORT_BODY_L7LIST_CLONE_COMM(p_st_private->p_st_body_data_req);
        if (p_st_private->p_lst_comm == NULL)
            goto ERROR;
        p_st_private->p_lst_req = FRREPORT_BODY_L7LIST_CLONE_REQ(p_st_private->p_st_body_data_req);
        if (p_st_private->p_lst_req == NULL)
            goto ERROR;
    }

    if (frreport_data_add_body(p_st_report_data, p_st_private->p_st_body_data_req) == FR_FAIL)
        goto ERROR;
    p_st_private->p_st_body_data_req = NULL;
    if (frreport_data_hash(p_st_hash_report, p_st_report_data) == FR_FAIL)
        goto ERROR;

    if (p_st_private->p_req_packet)
    {
        bytebuff_free(&p_st_private->p_req_packet);
        p_st_private->p_req_packet = NULL;
    }
    if (p_st_private->i_sign == 0)
    {
        mongodb_session_destroy((void *)(p_st_private));
        p_st_session->p_session_private = NULL;
    }
    return i_ret;

ERROR:
    if (p_st_report_data)
        frreport_data_free(&p_st_report_data);
    __mongodb_session_reset((void *)(p_st_private));
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * mongodb的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet       (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 * \param p_st_session       (私有数据)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *         APM_L7_ANALYZE_TODO
 *
 * \note
 *
 */
FR_STATIC int __resp_data_analysis(Conntrack *p_st_conn
                                 , Apm_packet *p_st_packet
                                 , Frhash_header *p_st_hash_report
                                 , L7_session *p_st_session)
{
    int i_flag = 0;
    int i_ret = APM_L7_ANALYZE_SUCC;
    int *p_i_tmp = NULL;
    unsigned long ul_tmp = 0;
    char arr_len[5];
    L7session_mongodb_private *p_st_private = NULL;
    L4_transaction *p_st_l4_trans = NULL;
    Frreport_data* p_st_report_data = NULL;

    if (p_st_conn == NULL || p_st_packet == NULL || p_st_hash_report == NULL || p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    memset(arr_len, 0x00, sizeof(arr_len));

    p_st_private = (L7session_mongodb_private*)p_st_session->p_session_private;
    p_st_l4_trans = &(p_st_private->st_l4_trans);

    if (p_st_private->p_st_body_data_resp == NULL)
    {
        //创建数据链表
        p_st_private->p_st_body_data_resp = frreport_body_new_l7(p_st_conn);
        if (p_st_private->p_st_body_data_resp == NULL)
            goto ERROR;

        if (p_st_l4_trans->st_response_time_first_byte.tv_sec == 0 && p_st_l4_trans->st_response_time_first_byte.tv_usec == 0)
        {
            p_st_l4_trans->st_response_time_first_byte.tv_sec = p_st_packet->st_timestamp.tv_sec;
            p_st_l4_trans->st_response_time_first_byte.tv_usec = p_st_packet->st_timestamp.tv_usec;
        }

        //创建bytebuff
        if (p_st_private->p_resp_packet == NULL)
        {
            p_st_private->p_resp_packet = bytebuff_creat();
            if (p_st_private->p_resp_packet == NULL)
                goto ERROR;
        }
    }

    /* 请求的字节数 */
    p_st_l4_trans->l_response_bytes += p_st_packet->ui_payload_len;

    if (p_st_private->p_resp_packet == NULL)
        goto ERROR;
    if (p_st_private->p_resp_packet->ui_remaining == 0)
    {
        arr_len[0] = *(p_st_packet->p_payload+3);
        arr_len[1] = *(p_st_packet->p_payload+2);
        arr_len[2] = *(p_st_packet->p_payload+1);
        arr_len[3] = *(p_st_packet->p_payload);
        p_i_tmp = (int *)arr_len;
        //预期数据总长度赋值给p_st_private->i_len
        p_st_private->i_len = ntohl(*p_i_tmp);
    }
    i_flag = p_st_private->p_resp_packet->ui_skip_len;
    //把这个包的数据先全加进bytebuff里
    if (bytebuff_add(p_st_private->p_resp_packet, p_st_packet->p_payload, p_st_packet->ui_payload_len) != FR_SUCC)
        goto ERROR;

    if (p_st_private->p_resp_packet->ui_skip_len)
        return APM_L7_ANALYZE_TODO;
    if (i_flag == 0)
    {
        //resp解析
        i_ret = __resp_parse(p_st_private->p_resp_packet, p_st_private, p_st_private->p_st_body_data_resp, p_st_conn);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;
        if (i_ret == APM_L7_ANALYZE_TODO)
        {
            frreport_body_free(&p_st_private->p_st_body_data_resp);
            p_st_private->p_st_body_data_resp=NULL;
            bytebuff_rewind(p_st_private->p_resp_packet);
            return i_ret;
        }
    }

    //数据上报
    if (p_st_l4_trans->st_request_time_first_byte.tv_sec != 0)
    {
        FRREPORT_BODY_L7LIST_SET_REQ(p_st_private->p_st_body_data_resp, &p_st_private->p_lst_req);
        FRREPORT_BODY_L7LIST_SET_COMM(p_st_private->p_st_body_data_resp, &p_st_private->p_lst_comm);
    }
    else
    {
        if (frreport_body_l7_add_connInfo(p_st_private->p_st_body_data_resp, p_st_conn) == FR_FAIL)
            goto ERROR;
    }
    //start time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_START_TIMESTAMP, &p_st_l4_trans->st_response_time_first_byte) == FR_FAIL)
        goto ERROR;

    if (p_st_l4_trans->st_request_time.tv_sec != 0 || p_st_l4_trans->st_request_time.tv_usec != 0)
    {
        //server time
        ul_tmp = frtime_diff(&(p_st_l4_trans->st_request_time), &(p_st_l4_trans->st_response_time_first_byte));
        if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_SERVER_TIME_US, ul_tmp) == FR_FAIL)
            goto ERROR;
    }
    else
    {
        if (frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_SERVER_TIME_US, 0) == FR_FAIL)
            goto ERROR;
    }

    //st_response_time
    p_st_l4_trans->st_response_time.tv_sec = p_st_packet->st_timestamp.tv_sec;
    p_st_l4_trans->st_response_time.tv_usec = p_st_packet->st_timestamp.tv_usec;

    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_DATA_BYTES, p_st_l4_trans->l_response_bytes) == FR_FAIL)
        goto ERROR;

    //down load time
    ul_tmp = frtime_diff(&(p_st_l4_trans->st_response_time_first_byte), &(p_st_l4_trans->st_response_time));
    if (frreport_body_l7_add_long(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_DOWNLOAD_TIME_US, ul_tmp) == FR_FAIL)
        goto ERROR;

    //end time
    if (frreport_body_l7_add_timeval(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_END_TIMESTAMP, &p_st_l4_trans->st_response_time) == FR_FAIL)
        goto ERROR;

    p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);
    if (p_st_report_data == NULL)
        goto ERROR;

    if (frreport_data_add_body(p_st_report_data, p_st_private->p_st_body_data_resp) == FR_FAIL)
        goto ERROR;
    p_st_private->p_st_body_data_resp = NULL;

    if (frreport_data_hash(p_st_hash_report, p_st_report_data) == FR_FAIL)
        goto ERROR;
    p_st_report_data = NULL;

    mongodb_session_destroy((void *)(p_st_private));
    p_st_session->p_session_private = NULL;
    return i_ret;

ERROR:
    if (p_st_report_data)
        frreport_data_free(&p_st_report_data);
    if (p_st_private->p_st_body_data_resp)
        frreport_body_free(&p_st_private->p_st_body_data_resp);
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * mongodb的packet解析
 *
 * \param p_st_conn          (入力链接信息)
 * \param p_st_packet        (入力packet信息)
 * \param p_st_hash_report   (保存Frreport_data)
 *
 * \return APM_L7_ANALYZE_SUCC   成功\n
 *         APM_L7_ANALYZE_FAIL   失败
 *
 * \note
 *
 */
int mongodb_analysis(Conntrack *p_st_conn
                           , Apm_packet *p_st_packet
                           , Frhash_header *p_st_hash_report)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    L7_session *p_st_session = NULL;

    if (p_st_conn == NULL || p_st_packet == NULL || p_st_hash_report == NULL)
        return APM_L7_ANALYZE_FAIL;

    /* 到Conntrack中取得session信息 */
    p_st_session = frct_get_session(p_st_conn);
    if (p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    //fin、syn包的话销毁私有数据
    if (p_st_packet->em_analyz_cmd == FRANALYZ_CMD_FIN || p_st_packet->em_analyz_cmd == FRANALYZ_CMD_SYN)
    {
        mongodb_session_destroy((void *)(p_st_session->p_session_private));
        p_st_session->p_session_private = NULL;
        return APM_L7_ANALYZE_SUCC;
    }

    //p_session_private分配内存
    if (p_st_session->p_session_private == NULL)
    {
        p_st_session->p_session_private = __mongodb_private_create();
        //内存分配失败
        if (p_st_session->p_session_private == NULL)
            return APM_L7_ANALYZE_FAIL;
    }

    //request
    if (p_st_packet->i_direction == FR_DIRECT_CLIENT2SRV)
    {
        i_ret = __req_data_analysis(p_st_conn,p_st_packet,p_st_hash_report,p_st_session);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;

        return i_ret;
    }
    else//response
    {
        i_ret = __resp_data_analysis(p_st_conn,p_st_packet,p_st_hash_report, p_st_session);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;

        return i_ret;
    }

ERROR:
    if (p_st_session->p_session_private)
        mongodb_session_destroy(p_st_session->p_session_private);
    p_st_session->p_session_private = NULL;
    return APM_L7_ANALYZE_FAIL;
}

/*!
 * 私有信息销毁
 *
 * \param p_private_session  (mongodb的私有session数据)
 *
 * \return FR_SUCC   成功\n
 *
 * \note
 *
 */
int mongodb_session_destroy(void *p_private_session)
{
    L7session_mongodb_private *p_st_private = (L7session_mongodb_private*)p_private_session;

    if (p_st_private)
    {
        if (p_st_private->p_st_body_data_req)
            frreport_body_free(&p_st_private->p_st_body_data_req);

        if (p_st_private->p_st_body_data_resp)
            frreport_body_free(&p_st_private->p_st_body_data_resp);

        if (p_st_private->p_lst_req)
            frreport_body_l7list_free(&p_st_private->p_lst_req);

        if (p_st_private->p_lst_comm)
            frreport_body_l7list_free(&p_st_private->p_lst_comm);

        if (p_st_private->p_req_packet)
            bytebuff_free(&(p_st_private->p_req_packet));

        if (p_st_private->p_resp_packet)
            bytebuff_free(&(p_st_private->p_resp_packet));

        frfree(p_st_private);
    }
    return FR_SUCC;
}
